package com.gopay.scheduler.job;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.gopay.common.user.manager.PersonThreeAccountChannelManager;
import org.apache.log4j.Logger;

import com.gopay.common.cipher.utils.LoggerMaskUtils;
import com.gopay.common.constants.trans.PayChannel;
import com.gopay.common.constants.trans.TxnStaCode;
import com.gopay.common.constants.txncd.IntTxnCd;
import com.gopay.common.domain.user.identify.PersCertChannelDTL;
import com.gopay.common.order.dao.CommonQueryDAO;
import com.gopay.common.user.dao.PersCertChannelDtlDao;
import com.gopay.remote.matcher.BankCodeQueryRemoteService;
import com.gopay.remote.matcher.vo.GopayBankInfoVO;
import com.gopay.scheduler.GopayBaseJob;


/**
 * 当天提现成功的卡号插入认证渠道流水表
 * PersonDrawCardJob.java
 * @author songfengli
 * 2016/5/30 上午9:35:16  www.gopay.com.cn Inc.All rights reserved.
 */

public class PersonDrawCardJob extends GopayBaseJob{
	
	private Logger logger = Logger.getLogger(this.getClass());
	 
	@Resource(name = "persCertChannelDtlDao")
    private PersCertChannelDtlDao persCertChannelDtlDao;
	
	@Resource(name = "commonQueryDAO")
    private CommonQueryDAO commonQueryDAO;
	
	@Resource(name = "bankCodeQueryClient")
    private BankCodeQueryRemoteService bankCodeQueryClient;

	@Resource(name = "personThreeAccountChannelManager")
	private PersonThreeAccountChannelManager personThreeAccountChannelManager;
	
	@Override
	protected void doJobInternal() {
		logger.error("PersonDrawCardJob------------start---------");
		logger.error("开始处理个人提现成功的银行信息！");
		//查询出当日个人提现成功的信息
		List<Object[]> personDrawCard=findPersonDrawCard();
		for(Object[] o : personDrawCard){
		  //返回对应银行卡code与银行卡号不为空的时候，插入认证日志表
		  if(o[2] != null && o[3] != null){
			  GopayBankInfoVO bankInfoResult=bankCodeQueryClient.getBankInfoByName(o[2].toString());
				  //判断认证日志表中有没有该用户的这个银行卡信息 ; 生产优化14期 修改 去掉渠道条件 只以银行卡为判断条件 只要该银行卡 有01 状态 不再插入
				  PersCertChannelDTL persCertChannelDtl=persCertChannelDtlDao.findByCardNoAndChannel(o[1].toString(),o[3].toString());
				  logger.error("该订单号是:"+o[4].toString()+",银行名称是:"+o[2].toString()+",银行卡号是:"+LoggerMaskUtils.maskString(o[3].toString())+"");
				  if(persCertChannelDtl == null){
					  PersCertChannelDTL PersCertChannel =new PersCertChannelDTL();//new一个新的domain
					  PersCertChannel.setCertNo(o[0]==null?"":o[0].toString());
					  PersCertChannel.setCustId(o[1].toString());
					  PersCertChannel.setChannelCode(bankInfoResult==null?o[2].toString():bankInfoResult.getCode());
					  PersCertChannel.setStatus("01");
					  PersCertChannel.setCreatedOperId(o[1].toString());
					  PersCertChannel.setCreatedTs(new Date());
					  PersCertChannel.setCardNo(o[3].toString());
					  PersCertChannel.setRemark("提现银行卡认证渠道");
					  logger.error("执行personThreeAccountChannelManager三类账号优化统一接口");
					  personThreeAccountChannelManager.savePersCertChannel(PersCertChannel);
					  logger.error("该订单号是:"+o[4].toString()+",银行名称是:"+o[2].toString()+",银行卡号是:"+LoggerMaskUtils.maskString(o[3].toString())+",往认证日志表中插入数据。");
				  }
		   }
		}
		logger.error("结束处理个人提现成功的银行信息！");
	}
	
	
	
	
	
	//查询出单日个人提现信息
	public List<Object[]> findPersonDrawCard(){
    	Map<String, Object> param = new HashMap<String, Object>();
    	param.put("payChannel", PayChannel._02.value);
    	param.put("intTxnCd", IntTxnCd._00800.value);
    	param.put("txnStaCode",String.valueOf(TxnStaCode.TXN_STA_CD_20000.value));
    	
    	SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Date date=new Date();
        String dateTime=sdf.format(date);
    	param.put("date",dateTime);
    	
    	StringBuffer sql = new StringBuffer(
    	 "select t3.cert_no,t3.cust_id,t2.recv_bank_name, t2.recv_bank_acct_num as card_no,t1.gopay_order_id" +
    	 " from cps_gen_main_order t1, cps_gen_pay_order t2, cust_person_info t3" +
    	 " where t1.gopay_order_id = t2.gopay_order_id and t1.pay_cust_id = t3.cust_id"+
    	 " and t1.pay_channel=:payChannel and t1.gopay_int_txn_cd=:intTxnCd and t1.txn_sta_cd=:txnStaCode"+
    	 " and to_char(tran_finish_time,'yyyy-MM-dd')=:date and t3.cert_no is not null ");
    	
    	return commonQueryDAO.findBySql(sql.toString(),param);	
    }

}
